package edu.hrbu.spring.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 徐登宇
 */
@Aspect // 定义切面类 表示为切面Bean
@Component // 交给Spring管理的Bean
public class MyAspect {

    @Pointcut("execution(public boolean edu.hrbu.spring.service.impl.OrderServiceImpl.generate())") // 切点表达式
    public void point() {
    }

    // 访问权限修饰符 返回值类型 全限定性类名 方法名 形参列表
    @Before("point()") // 前置通知
    public void beforeAdvice() {
        System.out.println("【MyAspect】的前置通知");
    }

    @After("point()") // 后置通知
    public void afterAdvice() {
        System.out.println("【MyAspect】的后置通知");
    }

    @AfterReturning(value = "point()", returning = "res") // 返回通知
    public void afterReturningAdvice(Object res) {
        System.out.println("【MyAspect】的返回通知, 返回结果为：" + res);
    }

    @AfterThrowing(value = "point()", throwing = "e") // 异常通知
    public void afterThrowingAdvice(Exception e) {
        String message = e.getMessage();
        System.out.println("【MyAspect】的异常通知，异常为：" + message);
    }

    @Around("point()") // 环绕通知
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("【MyAspect】的环绕通知开始");
        Object res = proceedingJoinPoint.proceed();// 启动目标方法执行
        System.out.println("【MyAspect】的环绕通知结束");
        return res;
    }

}
